<?php  

//是不是单
function isDan($temp) {
	if (count($temp) != 1) {
		return false;
	} else {
		return '{"type":"dan","value":"'.$temp[0].'"}';
	}
}

//是不是对
function isDui($temp) {
	if (count($temp) != 2 || $temp[0] != $temp[1]) {
		return false;
	} else {
		return '{"type":"dui","value":"'.$temp[0]*$temp[1].'"}';
	}
}

//是不是火箭
function isHuojian($temp) {
	if (count($temp) == 2 && (($temp[0] == 100 && $temp[1] == 99) || ($temp[0] == 99 || $temp[1] == 100))) {
		return '{"type":"huojian","value":"99999999"}';
	} else {
		return false;
	}
}

//是不是三个一样的
function isSan($temp) {
	if (count($temp) != 3 || ($temp[0] != $temp[1] || $temp[1] != $temp[2])) {
		return false;
	} else {
		return '{"type":"san","value":"'.$temp[0] * $temp[1] * $temp[2].'"}';
	}
}

//是不是炸弹
function isZha($temp) {
	if (count($temp) != 4 || ($temp[0] != $temp[1] || $temp[1] != $temp[2] || $temp[2] != $temp[3])) {
		return false;
	} else {
		return '{"type":"zhadan","value":"'.$temp[0] * $temp[1] * $temp[2] * $temp[3].'"}';
	}
}

//是不是三带一
function isSandaiyi($temp) {
	sort($temp);
	if (count($temp) == 4) {
		 if ($temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] != $temp[3]) {
		 	$value = $temp[0] * $temp[1] * $temp[2];
			return '{"type":"sandaiyi","value":"'.$value.'"}';
		 } else if ($temp[0] != $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
		 	$value = $temp[1] * $temp[2] * $temp[3];
			return '{"type":"sandaiyi","value":"'.$value.'"}';
		 } else {
		 	return false;
		 }
	} else {
		return false;
	}
}

//是不是三带二
function isSandaiEr($temp) {
	sort($temp);
	if (count($temp) == 5) {
		 if ($temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] != $temp[3] && $temp[3] == $temp[4]) {
		 	$value = $temp[0] * $temp[1] * $temp[2];
			return '{"type":"sandaier","value":"'.$value.'"}';
		 } else if ($temp[0] == $temp[1] && $temp[1] != $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
		 	$value = $temp[2] * $temp[3] * $temp[4];
			return '{"type":"sandaier","value":"'.$value.'"}';
		 } else {
		 	return false;
		 }
	} else {
		return false;
	}
}

//是不是顺子
function isShunzi($temp) {

	if (count($temp) < 5) {
		return false;
	}
	sort($temp);
	$value = 0;
	for ($i = 0; $i < count($temp) - 1; $i++) {
		if ($temp[$i+1] - $temp[$i] != 1) {
			return false;
		}
		$value = $value + $temp[$i];
	}

	return '{"type":"shunzi","value":"'.$value.'"}';
}

//是不是连对
function isLiandui($temp) {
	sort($temp);

	if (count($temp) < 6 || count($temp) % 2 != 0) {
		return false;
	}

	for ($i = 0; $i < count($temp) - 1; $i = $i + 2) {
		if ($temp[$i] != $temp[$i+1]) {
			return false;
		}

		if ($i < count($temp) - 2) {
			if ($temp[$i] - $temp[$i+2] != -1) {
				return false;
			}
		}
	}

	$value = 0;
	for ($i = count($temp) - 1; $i >= 0; $i--) {
		$value = $temp[$i] + $value;
	}

	return '{"type":"liandui","value":"'.$value.'"}';
}

//是不是飞机带单
function isFeijiDaiDan($temp) {
	if (count($temp) < 8) {
		return false;
	}
	$arrs = array_count_values($temp);
	$list = [];

	foreach ($arrs as $k => $v) {
		if ($v == 3) {
			$list[] = $k;
		}
	}

	sort($list);
	$value = array_product($list);
	for ($i=0; $i < count($list) - 1; $i++) { 
		if ($list[$i] - $list[$i + 1] != -1) {
			return false;
		}
	}

	$pokers = count($list) * 3 + count($list);

	if ($pokers != count($temp)) {
		return false;
	}

	return '{"type":"feijidaidan","value":"'.$value.'"}';
}

//是不是飞机带双
function isFeijiDaiShuang($temp) {
	if (count($temp) < 8) {
		return false;
	}
	$arrs = array_count_values($temp);
	$list = [];

	foreach ($arrs as $k => $v) {
		if ($v == 3) {
			$list[] = $k;
		}
	}

	sort($list);
	$value = array_product($list);
	for ($i=0; $i < count($list) - 1; $i++) { 
		if ($list[$i] - $list[$i + 1] != -1) {
			return false;
		}
	}

	$pokers = count($list) * 3 + count($list) * 2;

	if ($pokers != count($temp)) {
		return false;
	}

	$list = [];
	foreach ($arrs as $k => $v) {
		if ($v == 2) {
			$list[] = $k;
		}
	}

	$pokers = count($list);
	if (count($list) != $pokers) {
		return false;
	}

	return '{"type":"feijidaishuang","value":"'.$value.'"}';
}

//是不是飞机不带
function isFeijiBuDai($temp) {
	sort($temp);
	if (count($temp) % 3 != 0 || count($temp) < 3) {
		return false;
	} else {
		$value = 0;
		$index = [];
		for ($i = 0; $i + 2 < count($temp); $i = $i + 3) {
			if ($i != count($temp)) {
				if (!isSan([$temp[$i],$temp[$i+1],$temp[$i+2]])) {
					return false;
				}
				$value = $value + $temp[$i] + $temp[$i + 1] + $temp[$i+2];
				$index[] = $temp[$i];
			}
		}
		sort($index);
		for ($i=0; $i < count($index) - 1; $i++) { 
			if ($index[$i] - $index[$i + 1] != -1) {
				return false;
			}
		}

		return '{"type":"feijibudai","value":"'.$value.'"}';
	}
}

//是不是四带一
function isSiDaiYi($temp) {
	sort($temp);
	if (count($temp) == 5 && $temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
		$value = $temp[0] + $temp[1] + $temp[2] + $temp[3];
		return '{"type":"sidaiyi","value":"'.$value.'"}';
	} else if (count($temp) == 5 && $temp[1] == $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
		$value = $temp[1] + $temp[2] + $temp[3] + $temp[4];
		return '{"type":"sidaiyi","value":"'.$value.'"}';
	} else {
		return false;
	}

}


//是不是四带二
function isSiDaiEr($temp) {
	sort($temp);
	if (count($temp) == 6 && $temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
		$value = $temp[0] + $temp[1] + $temp[2] + $temp[3];
		return '{"type":"sidaier","value":"'.$value.'"}';
	} else if (count($temp) == 6 && $temp[1] == $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
		$value = $temp[2] + $temp[3] + $temp[4] + $temp[5];
		return '{"type":"sidaier","value":"'.$value.'"}';
	} else {
		return false;
	}
}

//检测牌型
function checkpokers($temp) {

	if (empty($temp)) {
		return false;
	}
	
	if (isDan($temp)) {
		return isDan($temp);
	} else if (isDui($temp)) {
		return isDui($temp);
	} else if (isHuojian($temp)) {
		return isHuojian($temp);
	} else if (isSan($temp)) {
		return isSan($temp);
	} else if (isZha($temp)) {
		return isZha($temp);
	} else if (isSandaiyi($temp)) {
		return isSandaiyi($temp);
	} else if (isShunzi($temp)) {
		return isShunzi($temp);
	} else if (isLiandui($temp)) {
		return isLiandui($temp);
	} else if (isFeijiBuDai($temp)) {
		return isFeijiBuDai($temp);
	} else if (isSiDaiYi($temp)) {
		return isSiDaiYi($temp);
	} else if (isSiDaiEr($temp)) {
		return isSiDaiEr($temp);
	} else if (isSandaiEr($temp)) {
		return isSandaiEr($temp);
	} else if (isFeijiDaiDan($temp)) {
		return isFeijiDaiDan($temp);
	} else if (isFeijiDaiShuang($temp)){
		return isFeijiDaiShuang($temp);
	} else {
		return false;
	}
	
}

//检测牌的大小
function checkdaxiao($round_type, $res)
{
	if ($round_type->type == 'huojian') {
		return true;
	}

	if ($res->type == 'zhadan') {
		if ($round_type->type == 'huojian') {
			return false;
		} else {
			return true;
		}
	}

	if ($round_type->type == $res->type && $res->value > $round_type->value) {
		return true;
	}

	return false;
}